package com.iemes.controller.hr;
/**
 * 员工档案
 */
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.CodeFormMap;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.PositionFormMap;
import com.iemes.entity.RecordFormMap;
import com.iemes.entity.StaffFormMap;
import com.iemes.mapper.hr.DepartmentinfoMapper;
import com.iemes.mapper.hr.RecordMapper;
import com.iemes.mapper.hr.StaffMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
@Controller
@RequestMapping("/hrrecord/")
public class RecordController extends BaseController{
   
	@Inject
	private StaffMapper staffMapper;
	@Inject
	private RecordMapper recordMapper;
	@Inject
    private DepartmentinfoMapper departmentinfoMapper;
	//档案
		@RequestMapping("recordAttend")
		public String examineOrder(Model model){
			DepartmentForMap departmentForMap=getFormMap(DepartmentForMap.class);
			departmentForMap.put("where", "where id!=16 ");
			//查部门名称
			List<DepartmentForMap> department=departmentinfoMapper.findByWhere(departmentForMap);
	        model.addAttribute("caset",department);
			model.addAttribute("res", findByRes());
			return Common.BACKGROUND_PATH + "/hr/attend_list";
		}
		//档案分页
		@ResponseBody
		@RequestMapping("record_findByPage")
		public PageView findByPage(String pageNow, String pageSize) throws Exception 
		{   
		
			// 实例化
			StaffFormMap staffFormMap = getFormMap(StaffFormMap.class);
			String full =getPara("full");
			if(full!=null&&full.equals("1")){
				staffFormMap.put("full", 1);
			}
			//查询方法
			List<Map<String,Object>> attendFormMaps=recordMapper.getAllRecord(staffFormMap);
			//自动存档
			StaffFormMap staffFormMapc = getFormMap(StaffFormMap.class);
			List<Map<String, Object>> staffFormMaps = staffMapper.getAllInfoation(staffFormMapc);	
			for(Map map:staffFormMaps){
				SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String dates=df.format(new Date());
				long date = df.parse(dates).getTime();//当前系统时间
				String entrys =map.get("first_entry").toString();
				String state =map.get("state").toString();
				long entry = df.parse(entrys).getTime();//员工入职时间
				long counts=(date-entry)/(1000 * 60 * 60 * 24L);
				//判断是否大于3个月
				if(counts>=90&&state.equals("1")){
					String id= map.get("id").toString();
					StaffFormMap staffForm = staffMapper.findbyFrist("id", id,
							StaffFormMap.class);
					String value = (staffForm.get("name").toString()).substring(0, 1);
					CodeFormMap codeFormMap = staffMapper.findbyFrist("value", value,
							CodeFormMap.class);
					String key = codeFormMap.get("key").toString();
					String file_number = key;
					// 通过档案外键查询档案号的最大值
					RecordFormMap recordFormMap = staffMapper.getAllRecordFormMap(file_number);
					RecordFormMap recordFormMapp = getFormMap(RecordFormMap.class);
					if(recordFormMap==null){
						String file_numbers=file_number+"-000";
						Integer number = Integer.valueOf(file_numbers.substring(file_numbers.indexOf("-") + 1, file_numbers.length()));
						String prefix = file_numbers.substring(0, file_numbers.indexOf("-") + 1);
						String record_File_number;
						if (number < 9) {
							record_File_number = prefix + "00" + (number + 1);
						} else if (number < 99) {
							record_File_number = prefix + "0" + (number + 1);
						} else {
							record_File_number = prefix + (number + 1);
						}
						recordFormMapp.put("file_number",record_File_number);
						recordFormMapp.put("state",1);
						recordMapper.addEntity(recordFormMapp);
						staffFormMapc.put("id", id);
						staffFormMapc.put("state", 3);
						staffFormMapc.put("archives_id", record_File_number);
						recordMapper.editEntity(staffFormMapc);
					}else{
						String file_numbers = recordFormMap.get("file_number").toString();
					Integer number = Integer.valueOf(file_numbers.substring(file_numbers.indexOf("-") + 1, file_numbers.length()));
					String prefix = file_numbers.substring(0, file_numbers.indexOf("-") + 1);
					String record_File_number;
					if (number < 9) {
						record_File_number = prefix + "00" + (number + 1);
					} else if (number < 99) {
						record_File_number = prefix + "0" + (number + 1);
					} else {
						record_File_number = prefix + (number + 1);
					}
					recordFormMap.put("file_number",record_File_number);
					recordFormMap.put("state",1);
					recordMapper.addEntity(recordFormMap);
					staffFormMapc.put("state", 3);
					staffFormMapc.put("archives_id", record_File_number);
					recordMapper.addEntity(staffFormMapc);
					}

					

				}
			}
			
			LuogicPaging(pageNow, pageSize, attendFormMaps);
			pageView.setRecords(attendFormMaps);
			return pageView;
		}

		
		//编辑档案
		@RequestMapping("record_edit")
		public String editUI(Model model) throws Exception 
		{
			String id = getPara("id");
			RecordFormMap recordFormMap=getFormMap(RecordFormMap.class);
			if(Common.isNotEmpty(id)){ 
				PositionFormMap positionFormMap=getFormMap(PositionFormMap.class);
				DepartmentForMap departmentForMap=getFormMap(DepartmentForMap.class);
				departmentForMap.put("where", "where id!=16 ");
				//查部门名称
				List<DepartmentForMap> department=departmentinfoMapper.findByWhere(departmentForMap);
		        model.addAttribute("caset",department);
				//查询职务名称
				positionFormMap.put("where", "where departid!=16");
		        List<PositionFormMap> position=recordMapper.findByWhere(positionFormMap);
		        //通过拿到的id来查询员工，部门，职务信息
		        StaffFormMap staffFormMaps = recordMapper.findbyFrist("id", id, StaffFormMap.class);
		        //拿到档案信息
				 recordFormMap =recordMapper.getAllArchives(staffFormMaps);
				model.addAttribute("duty",position);
				model.addAttribute("staff", staffFormMaps);
				model.addAttribute("record",recordFormMap);

			}

			return Common.BACKGROUND_PATH + "/hr/record_edit";
		}
		//修改档案
		@ResponseBody
		@RequestMapping(value="record_editEntity")
		@SystemLog(module="档案数据维护",methods="查看档案-修改档案")//凡需要处理业务逻辑的.都需要记录操作日志
		public String editEntity() {
			//通过档案号修改档案信息
			String file_number= getPara("recordFormMap.file_number");
			RecordFormMap recordFormMap=getFormMap(RecordFormMap.class);
			StaffFormMap staffFormMap = getFormMap(StaffFormMap.class);
			recordFormMap.put("file_number", file_number);
			try {
				recordMapper.editEntity(staffFormMap);
				recordMapper.getRecord(recordFormMap);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "false";
			}
			return "success";
		}
		
		
}
